package com.zzu.wardrobe.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zzu.wardrobe.entity.Clothes;
import com.zzu.wardrobe.service.ClothesServiceImpl;
import com.zzu.wardrobe.util.JsonUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@SuppressWarnings("all")
public class ClothesAction extends ActionSupport implements ModelDriven<Clothes> {
	private Clothes clothes = new Clothes();
	private List<Clothes> parks;
	private String str;
	private String domain;
	private String key;
	private String search;
	private File upload;
	private String uploadFileName;
	private String uploadContentType;
	private ClothesServiceImpl clothesService;
	Map<String, Object> res = new HashMap<String, Object>();
	HttpServletResponse response = ServletActionContext.getResponse();

	public Map<String, Object> getRes() {
		return res;
	}

	public void setRes(Map<String, Object> res) {
		this.res = res;
	}

	public ClothesServiceImpl getclothesService() {
		return clothesService;
	}

	public void setclothesService(ClothesServiceImpl clothesService) {
		this.clothesService = clothesService;
	}

	public Clothes getClothes() {
		return clothes;
	}

	public void setClothes(Clothes clothes) {
		this.clothes = clothes;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

	public String getDomain() {
		return domain;
	}

	public void setDomain(String domain) {
		this.domain = domain;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}

	@Override
	public Clothes getModel() {
		// TODO Auto-generated method stub
		return clothes;
	}

	public List<Clothes> getParks() {
		return parks;
	}

	public void setParks(List<Clothes> parks) {
		this.parks = parks;
	}

	public String outlist() {
		setParks(clothesService.showAll());
		return "outlistok";
	}

	public String out() {
		setParks(clothesService.showAll());

		return "outok";
	}

	public String out_show() {
		return "showok";
	}
	public String show() {
		HttpServletResponse response = ServletActionContext.getResponse();
		List<Clothes> list = clothesService.showAll();
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		// 允许跨域
		response.setHeader("Access-Control-Allow-Origin", "*");
		PrintWriter out;
		res.put("code", 0);
		res.put("content", list);// 这个就是个对象，所以不能用String，要用list**
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// Gson 传输数据、
		// start
//		JSONArray msg = JSONArray.fromObject(res);//这里转换成json对象
		JSONObject msg = JSONObject.fromObject(res);//这里转换成json数组对象

		// String msg = gson.toJson(res);
		try {
			System.out.println("***" + msg.toString());
			response.getWriter().print(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public String add() {

		/**
		 * 允许跨域
		 */
		response.setContentType("text/html;charset=utf-8");
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Headers", "*");
		Gson gson = new Gson();
		try {
			System.out.println("***//*/*" + ActionContext.getContext().getSession().get("username"));
			if (ActionContext.getContext().getSession().get("username") != null) {
				String jsoon = JsonUtil.getStrResponse();
				JSONObject jsonObject = JSONObject.fromObject(jsoon);
				String str = gson.toJson(clothes);

				System.out.println("jsonObject:" + jsonObject);
				Clothes clothesjson = (Clothes) JSONObject.toBean(jsonObject, Clothes.class);
				clothes.setId(jsonObject.getString("Temperature"));
				clothes.setTemperature(jsonObject.getString("Temperature"));
				clothes.setHumidness(jsonObject.getString("Humidness"));
				System.out.println("clothesjson:" + clothesjson);
				System.out.println(clothesService.add(clothes));
				System.out.println(clothes.toString());
				res.put("code", 0);
				res.put("msg", "添加成功！");
			} else {
				res.put("code", 2);
				res.put("msg", "您未登录，请去登录！");
			}
			String msg = gson.toJson(res);
			response.getWriter().print(msg);
		} catch (Exception e) {
			System.out.println(e);
		}
		// InputStream input = null;
		// OutputStream output = null;
		// if (!this.uploadContentType.equals("image/jpeg")) {
		// return "addok";
		// } else {
		//
		//
		// // try {
		// // Date date = new Date();
		// // uploadFileName =
		// // date.getTime()+uploadFileName.substring(uploadFileName.lastIndexOf('.'));
		// // System.out.println(ActionContext.getContext());
		// // input = new FileInputStream(upload);
		// // String path =
		// // ServletActionContext.getServletContext().getRealPath("/upload");
		// // output = new FileOutputStream(path+"\\"+this.uploadFileName);
		// // byte[] con = new byte[512];
		// // int len = 0;
		// // while(true){
		// // if((len=input.read(con))==-1){
		// // break;
		// // }
		// // output.write(con,0,len);
		// // }
		// // output.close();
		// // input.close();
		// // }catch (Exception e) {
		// // }
		// }15837195819
		return null;
	}
}
